package com.csun.dsms.service.impl;

import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.mapper.DsmsHealthReportMapper;
import com.csun.dsms.model.domain.*;
import com.csun.dsms.model.dto.healthReport.HealthReportDTO;
import com.csun.dsms.model.dto.healthReport.HealthReportVO;
import com.csun.dsms.service.*;
import com.csun.dsms.utils.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DsmsHealthReportServiceImpl extends BaseService<DsmsHealthReport> implements DsmsHealthReportService {

    @Resource
    private DsmsHealthReportMapper healthReportMapper;

    @Override
    public PageInfo queryListWithPage(HealthReportDTO healthReportDTO) {
        Integer pageSize = healthReportDTO.getPageSize();
        int test = this.test(healthReportDTO);

        if (healthReportDTO.getQueryDate() != null) {
            healthReportDTO.setQueryDateEnd(healthReportDTO.getQueryDate() + " 23:59:59");
        }

        healthReportDTO.setPageSize(pageSize);
        PageHelper.startPage(healthReportDTO.getPageNum(), healthReportDTO.getPageSize());

        List<DsmsHealthReport> dsmsHealthReports = healthReportMapper.list(healthReportDTO);

        List<HealthReportVO> healthReportVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dsmsHealthReports)) {
            for (DsmsHealthReport dsmsHealthReport : dsmsHealthReports) {
                HealthReportVO healthReportVO = new HealthReportVO();
                BeanUtils.copyProperties(dsmsHealthReport, healthReportVO);
                healthReportVO.setCreateTime(DateUtil.dateToStr(dsmsHealthReport.getCreateTime()));
                healthReportVOS.add(healthReportVO);
            }
        }
        PageInfo pageInfo = new PageInfo<>(healthReportVOS);
        pageInfo.setTotal(test);
        return pageInfo;
    }

    private int test(HealthReportDTO healthReportDTO){
        if (healthReportDTO.getQueryDate() != null) {
            healthReportDTO.setQueryDateEnd(healthReportDTO.getQueryDate() + " 23:59:59");
        }

        healthReportDTO.setPageSize(null);
        List<DsmsHealthReport> dsmsHealthReports = healthReportMapper.list(healthReportDTO);
        return dsmsHealthReports.size();
    }
}
